package com.example.Lezedata.dataModel;

import android.view.View;

import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import bid.Bid;
import common.Common;

public class BidDetailManager {
    private static BidDetailManager manager;
    private static HashMap<String,Bid.GoodsInfo> goodsInfoHashMap = new HashMap<String,Bid.GoodsInfo>();
    public interface OnLoadListDataComplete{
        void listInfoResponse(List<Bid.GoodsInfo> goodsInfoList);
    }

    public interface OnLoadDataComplete{
        void infoResponse(Bid.GoodsInfo goodsInfo);
    }

    public interface OnLoadBidDetailDataComplete{
        void infoResponse(Bid.BidDetailResponse response);
    }

    public OnLoadListDataComplete onLoadListDataComplete;
    public OnLoadDataComplete onLoadDataComplete;
    public OnLoadBidDetailDataComplete onLoadBidDetailDataComplete;

    public static BidDetailManager sharedManager(){
        if (manager == null){
            manager = new BidDetailManager();
        }
        return manager;
    }

    public static void getGoodsInfoForIds(List<String> ids, final OnLoadListDataComplete complete){
        Common.IdList list = Common.IdList.newBuilder().addAllList(ids).build();
        GBNetRequest.stub().batchGetGoodsInfo(list,
                ServerResponseObserver.observer(new ServerResponseObserver.requestListener<Bid.BatchGetGoodsInfoResponse>() {
            @Override
            public void complete(Bid.BatchGetGoodsInfoResponse value, GBError error) {
                if (error == null){
                    if (complete != null){
                        for (Bid.GoodsInfo goodsInfo:value.getListList()
                             ) {
                            goodsInfoHashMap.put(goodsInfo.getGoodsId(),goodsInfo);
                        }
                        complete.listInfoResponse(value.getListList());
                    }
                }
            }
        }));
    }

    public static void getGoodsInfoForId(String id,final OnLoadDataComplete complete){
        if (id == null){
            return;
        }
        final Bid.GoodsInfo goodsInfo = goodsInfoHashMap.get(id);
        if (goodsInfo != null){
            if (complete != null) {
                complete.infoResponse(goodsInfo);
            }
            return;
        }
        ArrayList<String> list = new ArrayList<String>();
        list.add(id);
        getGoodsInfoForIds(list, new OnLoadListDataComplete() {
            @Override
            public void listInfoResponse(List<Bid.GoodsInfo> goodsInfoList) {
                if (complete != null) {
                    if (goodsInfoList.size() > 0){
                        complete.infoResponse(goodsInfoList.get(0));
                    }
                }
            }
        });
    }

    public static void getBidInfoForId(String id,final OnLoadBidDetailDataComplete complete){
        if (id == null){
            return;
        }
        Common.Id bid = Common.Id.newBuilder().setId(id).build();
        ServerResponseObserver observer = new ServerResponseObserver(new ServerResponseObserver.requestListener<Bid.BidDetailResponse>() {
            @Override
            public void complete(Bid.BidDetailResponse value, GBError t) {
                if (t == null){
                    if (complete != null){
                        complete.infoResponse(value);
                    }
                }
            }
        });
        GBNetRequest.stub().bidDetail(bid,observer);
    }
}